--[[
  杭州银行 - 核心前置
]]
require("apm_core")
local ApmXml = require("ApmXml")

local G_DEBUG = true

if G_DEBUG == false then
    function logDebug(fmt,...) end
    function logInfo(fmt,...) end
    function logWarn(fmt,...) end
    function logError(fmt,...) end
    function logFatal(fmt,...) end
end

local KRED='\x1B[0;31m'
local KSALMON='\x1B[1;31m'
local KGREEN='\x1B[0;32m'
local KLIMEGREEN='\x1B[1;32m'
local KYELLOW='\x1B[0;33m'
local KORANGE='\x1B[0;34m'
local KBLUE='\x1B[1;34m'
local KMAGENTA='\x1B[0;35m'
local KPINK='\x1B[1;35m'
local KCYAN='\x1B[0;36m'
local RESET='\x1B[0m'

local t_xml_tree
local s_txcode
local s_txcode_name
local s_txchan
local s_txchan_name
local s_tranSerialNo
local s_rspcode
local s_rspmsg

local t_txcode = {}
local t_txchan = {}

local function detect(s_payload)
    if s_payload == nil then return false end

    t_xml_tree = ApmXml.load(s_payload, "UTF-8")

    return ApmXml.getValue(t_xml_tree, "/ebank") ~= nil
end

--- 解析请求
-- return
-- true: 解析成功; false: 解析失败
local function parse_req()
    s_txcode = ApmXml.getValue(t_xml_tree, "/ebank/common/TxCode/text()")
    if s_txcode ~= nil then
        s_txcode = string.trim(s_txcode)
        s_txcode_name = t_txcode[s_txcode]
        if s_txcode_name == nil then s_txcode_name = s_txcode end
    end

    s_txchan = ApmXml.getValue(t_xml_tree, "/ebank/common/TxChan/text()")
    if s_txchan ~= nil then
        s_txchan = string.trim(s_txchan)
        if string.len(s_txchan) > 2 then
            logWarn("channel too long:[%s]", s_txchan)
            s_txchan = string.sub(s_txchan, -2)
        end
        s_txchan_name = t_txchan[s_txchan]
        if s_txchan_name == nil then s_txchan_name = s_txchan end
    end

    s_tranSerialNo = ApmXml.getValue(t_xml_tree, "/ebank/common/tranSerialNo/text()")
    return true
end

--- 解析响应
-- return
-- true: 解析成功; false: 解析失败
local function parse_resp()
    s_rspcode = ApmXml.getValue(t_xml_tree, "/ebank/response/Result/text()")
    s_rspmsg = ApmXml.getValue(t_xml_tree, "/ebank/response/ResultMsg/text()")
    return true
end

function post_analysis()
    local ApmData = require("ApmData")
    if ApmData.getL7Proto() ~= 50003 then return end
    if ApmData.getMsgType() ~= 1 then return end

    local s_req
    local s_resp

    s_req = ApmData.getField("request.xml.message", 1)
    if s_req ~= nil then
        s_req = string.trim(s_req)
        if detect(s_req) then
            parse_req()
        end
    end

    s_resp = ApmData.getField("response.xml.message", 1)
    if s_resp ~= nil then
        s_resp = string.trim(s_resp)
        if detect(s_resp) then
            parse_resp()
        end
    end
	
    if s_txcode == nil then
        logInfo("s_txcode is nil REQ:[%s]", s_req)
    elseif s_rspcode == nil then
        logInfo("s_rspcode is nil RESP:[%s]", s_resp)
    else
        ApmData.replaceField("transaction.type_code", s_txcode)
        ApmData.replaceField("transaction.type_name", s_txcode_name)
        ApmData.replaceField("transaction.title", s_txcode_name)

        if s_txchan ~= nil then
            ApmData.replaceField("transaction.channel_code", s_txchan)
            ApmData.replaceField("transaction.channel_name", s_txchan_name)
        end

        if s_tranSerialNo ~= nil then
            ApmData.replaceField("transaction.code", s_tranSerialNo)
        end

        ApmData.replaceField("response.code_string", s_rspcode)
        ApmData.delFields("response.code")

        if s_rspmsg ~= nil then
            ApmData.replaceField("response.error_message", s_rspmsg)
        end

        -- 为了降低ES的压力，返回码正常的情况下删除body内容
--        if s_rspcode ~= "0" and s_rspcode ~= "00" and s_rspcode ~= "000000" then
--            ApmData.delFields("request.xml.message")
--            ApmData.delFields("response.xml.message")
--        end

        logInfo("核心前置(%s:%d->%s:%d) name:%s|channel:%s|seq:%s|ret:%s|%s",
            ApmData.getSrcIp(),
            ApmData.getSrcPort(),
            ApmData.getDstIp(),
            ApmData.getDstPort(),
            s_txcode_name,
            s_txchan_name,
            s_tranSerialNo,
            s_rspcode,
            s_rspmsg
        )
    end

    return
end
--核心前置
--192.168.67.111:8818
--192.168.67.31:8818
--v1.0.5
